package com.lyh.hkcloud.module.m1300025.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.module.common.MyDialog;
import com.lyh.hkcloud.module.m1300084.IndexActivity;

import java.util.List;
import java.util.Map;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 数据适配器
 * @company—date: 利元数据 2022-02-14 15:12
 **/

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String,Object>> dataMap;
    private int saveposition;
    private DeleteCallback Callback;
    private IndexActivity mContext;

    /**
     * 构造器
     *
     * @param context  上下文对象
     * @param dataMap 数据
     */
    public ListViewAdapter(Context context,List<Map<String,Object>> dataMap,IndexActivity mContext) {
        this.context = context;
        this.dataMap = dataMap;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return dataMap.size();
    }

    @Override
    public Object getItem(int position) {
        return dataMap.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v("position---:",String.valueOf(position));

        ViewHolder viewHolder;
        position = 0;
        //判断是否有缓存
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_listview_item_1300084, null);
            viewHolder = new ViewHolder(convertView);
            //实例化item获取控件
            viewHolder.delete_image = convertView.findViewById(R.id.delete_image);

            convertView.setTag(viewHolder);
        } else {
            //得到缓存的布局
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Log.v("dataMap",dataMap.toString());

            viewHolder.facard_code.setText(dataMap.get(position).get("facardcode") == null ? "" : dataMap.get(position).get("facardcode").toString());
            viewHolder.ma_name.setText(dataMap.get(position).get("maname") == null ? "" : dataMap.get(position).get("maname").toString());
            viewHolder.ma_number.setText(dataMap.get(position).get("manumber") == null ? "" : dataMap.get(position).get("manumber").toString());


        /*删除扫描资产*/
        viewHolder.delete_image.setTag(position);

        viewHolder.delete_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyDialog.show(mContext, "确定删除吗?", new MyDialog.OnConfirmListener() {

                    @Override
                    public void onConfirmClick() {
                        saveposition = Integer.parseInt(v.getTag().toString());
                        Callback.deletePosition(saveposition);
                    }
                });
            }
        });

        return convertView;
    }

    public interface DeleteCallback { void deletePosition(int saveposition);}

    public void setDeleteCallback(DeleteCallback Callback) {this.Callback = Callback;}

    public void setData(List<Map<String,Object>> data2) {
        this.dataMap = data2;
    }

    /**
     * ViewHolder类
     */
    private final class ViewHolder {
        //资产编码
        TextView facard_code;
        //物料描述
        TextView ma_name;
        //物料编码
        TextView ma_number;
        //删除按钮
        ImageView delete_image;

        /**
         * 构造器
         *
         * @param view 视图组件（ListView的子项视图）
         */
        ViewHolder(View view) {
            facard_code =  view.findViewById(R.id.facard_code);
            ma_name =  view.findViewById(R.id.ma_name);
            ma_number = view.findViewById(R.id.ma_number);
            delete_image = view.findViewById(R.id.delete_image);
        }
    }
}

